I was tempted to just leave it at that, but I'll say a bit more. In my experience, most (anything beyond a trivial problem and most trivial problems) of the time I use a collection, at some point I'll want more behavior that the collection offers. The collection implements a "raw" zero-to-many relationship. Most of the time, my requirements want more than just a "raw" relationship. In the case of the RpnCalculator, it's stack is of infinite size and it always has zero after anything else that is pushed onto it. We simulated this by returning 0 if the stack is in fact empty.
It is nearly always a good idea to augment a system-defined collection with your own flair.
When you do, you have several options:
Subclass (in .Net you don't have this option because most methods are sealed and non-virtual)
Wrap and delegate (this is what we did). You always have this option
Open up the class (Ruby and other dynamic languages) - this is dangerous. Opening up a heavily used class like this is "too cool."
Open up an instance (shown above) - this is probably too cool as well.
The option that always works, though it might require a touch more work, is the second option and what you did in this tutorial.
If you every have a collection within another collection, it's immediately time to follow this recommendation.
Do it!
I was tempted to just leave it at that, but I'll say a bit more. In my experience, most (anything beyond a trivial problem and most trivial problems) of the time I use a collection, at some point I'll want more behavior that the collection offers. The collection implements a "raw" zero-to-many relationship. Most of the time, my requirements want more than just a "raw" relationship. In the case of the RpnCalculator, it's stack is of infinite size and it always has zero after anything else that is pushed onto it. We simulated this by returning 0 if the stack is in fact empty.
It is nearly always a good idea to augment a system-defined collection with your own flair.
When you do, you have several options:
The option that always works, though it might require a touch more work, is the second option and what you did in this tutorial.
If you every have a collection within another collection, it's immediately time to follow this recommendation.